package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Task;
import cn.wolfcode.crm.domain.TaskExecute;
import cn.wolfcode.crm.mapper.TaskExecuteMapper;
import cn.wolfcode.crm.mapper.TaskMapper;
import cn.wolfcode.crm.query.impl.TaskExecuteQuery;
import cn.wolfcode.crm.service.ITaskExecuteService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class TaskExecuteServiceImpl implements ITaskExecuteService {
    @Autowired
    private TaskExecuteMapper mapper;
    @Autowired
    private TaskMapper taskMapper;


    public PageResult query(TaskExecuteQuery qo) {
        int rows = mapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<TaskExecute> data = mapper.queryForList(qo);
        return new PageResult(rows, data);
    }

    public void saveOrUpdate(Task task,Long employeeId,Long tid) {
        if(tid==null){
            taskMapper.insert(task);
            Employee employee = new Employee();
            TaskExecute taskExecute = new TaskExecute();
            employee.setId(employeeId);
            taskExecute.setData(new Date());
            taskExecute.setEmployee(employee);
            taskExecute.setState(false);
            taskExecute.setTask(task);
            mapper.insert(taskExecute);
        }else{
            taskMapper.updateByTaskno(task);
            TaskExecute taskExecute = new TaskExecute();
            taskExecute.setData(new Date());
            taskExecute.setTid(tid);
            mapper.updateByPrimaryKey(taskExecute);
        }
    }

    
    public void updatestate(Long tid) {
        mapper.updatestate(tid);
    }

    public void deleteByTid(Long tid) {
        mapper.deleteByPrimaryKey(tid);
    }

    @Override
    public List<TaskExecute> selecteAll() {
        return mapper.selectAll();
    }

    @Override
    public void exportXls(HttpServletResponse response)throws IOException{
        //文件下载响应头
        response.setHeader("Content-disposition", "attachment;filename=xixi.xls");
        //创建excel文件(workbook)
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("day01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("部门");
        row.createCell(3).setCellValue("任务编号");
        row.createCell(4).setCellValue("任务时间");
        row.createCell(5).setCellValue("任务地点");
        row.createCell(6).setCellValue("任务内容");
        //查询员工数据
        List<TaskExecute> list = mapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            TaskExecute taskExecute = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(taskExecute.getData());
            row.createCell(1).setCellValue(taskExecute.getEmployee().getUsername());
            row.createCell(2).setCellValue(taskExecute.getEmployee().getDept().getName());
            row.createCell(3).setCellValue(taskExecute.getTask().getTaskno());
            row.createCell(4).setCellValue(taskExecute.getTask().getDate());
            row.createCell(5).setCellValue(taskExecute.getTask().getSite());
            row.createCell(6).setCellValue(taskExecute.getTask().getTaskword());
        }
        //写入数据
        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
